/**
 * 添加或者修改页面
 */
var OrderPutDetailedInfoDlg = {
    data: {
        detailedGoodsname: "",
        detailedGoodsnum: "",
        goodsNumber: "",
		goodsNo:"",

        detailedRebubbles: "",
        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        goodsUnit: "",
        goodsSpecs: "",

        goodsWeight: "",
        goodsVolume: "",
        goodsCharging: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    
    //选择生产日期后，填入批次 
    laydate.render({
    	elem:'#detailedDateofmanufacture',
    	/*done: function(value, date, endDate){
      	    var detailedDate = value.replace(/[-]/g,"");//得到日期生成的值，如：2017-08-18,去除-
      	    $("#detailedBatchnumber").val(detailedDate);
      	}*/
    })
    laydate.render({
    	elem:'#recommend'
    })
    
    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/orderPutDetailed/addItem", function (data) {
        	if (data.success) {
	            Feng.success("Add successful!");
	        	var Index = parent.layer.getFrameIndex(window.name);
	            parent.layer.close(Index);
			} else {
                Feng.error("Add failed!"+data.message);
			}
        }, function (data) {
            Feng.error("Add failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    //除以的精确结果
    function accDiv(arg1,arg2){  
	　　var t1=0,t2=0,r1,r2;  
	　　try{
	　　　　t1=arg1.toString().split(".")[1].length;
	　　}catch(e){}

	　　try{
	   　　t2=arg2.toString().split(".")[1].length;
	　　}catch(e){}

	　　with(Math){
	　　　　r1=Number(arg1.toString().replace(".","")); 
	   　　r2=Number(arg2.toString().replace(".","")); 
	　　　　return (r1/r2)*pow(10,t2-t1);  
	　　}
    };
    
    //乘以的精确结果
    function accMul(arg1,arg2){  
    	var m=0,s1=arg1.toString(),
    	s2=arg2.toString();  
    	try{
    	m+=s1.split(".")[1].length}catch(e){}  
    	try{
    	m+=s2.split(".")[1].length}catch(e){}  
    	return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m
    )};
    
    //点击商品名称
    $("#detailedGoodsname").click(function(){
        layer.open({
            type: 2,
            title: 'Select Product',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/orderPutDetailed/toChoiceGoods?goodsCustomerId=' + $("#putCustomerId").val(),
            end: function () {
                $("#detailedGoodsname").val(OrderPutDetailedInfoDlg.data.detailedGoodsname);//商品名称
                $("#detailedGoodsnum").val(OrderPutDetailedInfoDlg.data.detailedGoodsnum);//商品id
                $("#goodsNumber").val(OrderPutDetailedInfoDlg.data.goodsNumber);//商品编号
                $("#goodsNo").val(OrderPutDetailedInfoDlg.data.goodsNo);//货号

                $("#detailedRebubbles").val(OrderPutDetailedInfoDlg.data.detailedRebubbles);//重泡货
                $("#goodsUnit").val(OrderPutDetailedInfoDlg.data.goodsUnit);
                $("#detailedLargeclass").val(OrderPutDetailedInfoDlg.data.detailedLargeclass);//商品大类
                $("#detailedGoodstype").val(OrderPutDetailedInfoDlg.data.detailedGoodstype);//商品类别
                $("#detailedPacking").val(OrderPutDetailedInfoDlg.data.detailedPacking);//商品包装
                $("#goodsSpecs").val(OrderPutDetailedInfoDlg.data.goodsSpecs);

                $("#goodsWeight").val(OrderPutDetailedInfoDlg.data.goodsWeight);//重量
                $("#goodsVolume").val(OrderPutDetailedInfoDlg.data.goodsVolume);//体积
                $("#detailedUnitprice").val(OrderPutDetailedInfoDlg.data.goodsCharging);//单价
            }
        });
    });
    
    //输入大包数量
    $("#detailedPutbalenum").keyup(function(){
    	var detailedGoodsname = $("#detailedGoodsname").val();
    	if (detailedGoodsname==null||detailedGoodsname=='') {
    		layer.msg('Please select a product'); 
		} else {
			var detailedPutbalenum = $("#detailedPutbalenum").val();//大包数量
			var goodsVolume = $("#goodsVolume").val();//体积
			var goodsWeight = $("#goodsWeight").val();//重量
			var detailedUnitprice = $("#detailedUnitprice").val();//单价
			$("#detailedPutnum").val(detailedPutbalenum);//改变应入数量
			$("#detailedPutvolume").val(accMul(detailedPutbalenum,goodsVolume));//改变应入体积
			$("#detailedPutweight").val(accMul(detailedPutbalenum,goodsWeight));//改变应入重量
			$("#detailedMoney").val(accMul(detailedPutbalenum,detailedUnitprice));//改变金额
		}
    });
    
    //输入应入数量
    $("#detailedPutnum").keyup(function(){
    	var detailedGoodsname = $("#detailedGoodsname").val();
    	if (detailedGoodsname==null||detailedGoodsname=='') {
    		layer.msg('Please select a product'); 
		} else {
			var detailedPutnum = $("#detailedPutnum").val();//应入数量
			var goodsVolume = $("#goodsVolume").val();//体积
			var goodsWeight = $("#goodsWeight").val();//重量
			var detailedUnitprice = $("#detailedUnitprice").val();//单价
			$("#detailedPutbalenum").val(detailedPutnum);//改变应入数量
			$("#detailedPutvolume").val(accMul(detailedPutnum,goodsVolume));//改变应入体积
			$("#detailedPutweight").val(accMul(detailedPutnum,goodsWeight));//改变应入重量
			$("#detailedMoney").val(accMul(detailedPutnum,detailedUnitprice));//改变金额
		}
    });
    
    //输入单价
    $("#detailedUnitprice").keyup(function(){
    	var detailedGoodsname = $("#detailedGoodsname").val();
    	if (detailedGoodsname==null||detailedGoodsname=='') {
    		layer.msg('请选择商品'); 
		} else {
			var detailedPutnum = $("#detailedPutnum").val();//应入数量
			var detailedUnitprice = $("#detailedUnitprice").val();//单价
			$("#detailedMoney").val(accMul(detailedPutnum,detailedUnitprice));//改变金额
		}
    });
    
    //输入金额
    $("#detailedMoney").keyup(function(){
    	var detailedGoodsname = $("#detailedGoodsname").val();
    	if (detailedGoodsname==null||detailedGoodsname=='') {
    		layer.msg('请选择商品'); 
		} else {
			var detailedPutnum = $("#detailedPutnum").val();//应入数量
			var detailedMoney = $("#detailedMoney").val();//金额
			$("#detailedUnitprice").val(accDiv(detailedMoney,detailedPutnum));//改变单价
		}
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});